package com.atguigu.srb.core.controller.api;


import com.atguigu.common.result.R;
import com.atguigu.srb.base.utils.JwtUtils;
import com.atguigu.srb.core.pojo.vo.BorrowerVO;
import com.atguigu.srb.core.service.BorrowerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 借款人 前端控制器
 * </p>
 *
 * @author tjf
 * @since 2022-01-03
 */
@RestController
@Api(tags = "借款人")
@RequestMapping("/api/core/borrower")
@Slf4j
public class BorrowerController {
    private final BorrowerService borrowerService;

    public BorrowerController(BorrowerService borrowerService) {
        this.borrowerService = borrowerService;
    }

    @ApiOperation("保存借款人信息")
    @PostMapping("/auth/save")
    public R save(@ApiParam("用户提交数据") @RequestBody BorrowerVO borrowerVO, @RequestHeader(required = false) String token) {
        // 确保用户已经登录,getUserId内部已做校验
        Long userId = JwtUtils.getUserId(token);
        borrowerService.saveBorrowerVOByUserId(userId, borrowerVO);
        return R.ok().message("信息提交成功");
    }

    @ApiOperation("获取借款人认证状态")
    @GetMapping("/auth/getBorrowerStatus")
    public R getBorrowerStatus(@RequestHeader(required = false) String token) {
        // 确保用户已经登录,getUserId内部已做校验
        Long userId = JwtUtils.getUserId(token);
        Integer status = borrowerService.getStatusByUserId(userId);
        return R.ok().data("borrowerStatus", status);
    }
}

